package com.cgwx.systemservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cgwx.springsecurity.entity.SysUser;
import com.cgwx.systemservice.entity.vo.RouterVo;
import com.cgwx.systemservice.entity.vo.SysUserQueryVo;
import com.cgwx.systemservice.mapper.SysUserMapper;
import com.cgwx.systemservice.service.SysMenuService;
import com.cgwx.systemservice.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author GaoWei
 * @since 2023-10-12
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysMenuService sysMenuService;

//    条件分页查询
    @Override
    public IPage<SysUser> selectPage(Page<SysUser> pageParam, SysUserQueryVo sysUserQueryVo) {
        return baseMapper.selectPage(pageParam,sysUserQueryVo);
    }

//    修改用户状态
    @Override
    public void updateStatus(String id, Integer status) {
        SysUser sysUser = baseMapper.selectById(id);
        sysUser.setStatus(status);
        baseMapper.updateById(sysUser);
    }

//    根据用户名称进行查询
    @Override
    public SysUser getUserInfoByUserName(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return baseMapper.selectOne(queryWrapper);
    }

//    查询用户信息
    @Override
    public Map<String, Object> getUserInfo(String username) {
//        根据用户名称查询用户基本信息
        SysUser sysUser = this.getUserInfoByUserName(username);
//        根据用户id查询菜单权限
        List<RouterVo> routeVoList = sysMenuService.getUserMenuList(sysUser.getId());
//        根据用户id查询按钮权限
        List<String> permsList = sysMenuService.getUserButtonList(sysUser.getId());
//        封装数据
        Map<String, Object> result = new HashMap<>();
        result.put("name",username);
        result.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
//        这个没啥用，前端给删掉了
//        result.put("roles","[admin]");
        result.put("buttons", permsList);
        result.put("routers", routeVoList);
        return result;
    }
}
